﻿using System;
using Model;
using Resources;
using Service;

public partial class employeeAdd : System.Web.UI.Page
{
    private int employeeId = 0;
    private Employee _employee = new Employee();
    private bool _flagNew = false;
    readonly ReferenceDataService _ReferenceDataService = new ReferenceDataService();
    readonly StockService _StockService = new StockService();

    protected void Page_Load(object sender, EventArgs e)
    {
        int sessionId = Session["employeeId"] != null ? Int32.Parse(Session["employeeId"].ToString()):0;
        employeeId = GetEmployeeId();
        pnlBody.Visible = employeeId == 0 ? false : true;
        if (employeeId == 0) return;

        if(!IsPostBack)
        {

            employeeId = GetEmployeeId();
            if (employeeId != 0) DisplayDetailInfo(employeeId);

        }

    }
    
    public int GetEmployeeId()
    {
        if(!_flagNew )
            return !String.IsNullOrEmpty(Request.QueryString["employeeId"]) ? Int32.Parse(Request.QueryString["employeeId"]) : 0;
        return 0;
    }
    
    private void DisplayDetailInfo(int emplId)
    {

        _employee = _StockService.GetEmployee(emplId);
        if (_employee != null)
        {
            txtEmpName.Text = _employee.EmployeeName;
            txtEmpEmail.Text = _employee.EmailAddress;
            txtLogin.Text = _employee.UserName;
            txtPayroll.Text = _employee.Payroll;
            txtDomain.Text = _employee.FullDomain;
            chb1.Checked = _employee.ReportInvisible != null ? _employee.ReportInvisible : false;
            drlGroup.SelectedValue = _employee.GroupId != null ?_employee.GroupId.ToString() : "-1";
            drlDepartment.SelectedValue = _employee.DepartmentId != null ? _employee.DepartmentId.ToString() : "-1";
            drlCompany.SelectedValue = _employee.CompanyId != null ?_employee.CompanyId.ToString(): "-1";
            
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!new Utility().TestPermissions(Resource.PermissionEditEmployee))
        {
            lblPermission.Text = Resource.Permission;
            employeeId = GetEmployeeId();
            DisplayDetailInfo(employeeId);
            return;
        }
        if (drlDepartment.SelectedIndex == 0)
        {
            lblRequiredDepartment.Text = Resource.MessInformSelectAValue;
            lblRequiredDepartment.Visible = true;
            return;
        }
        lblRequiredDepartment.Visible = false;
        if (drlCompany.SelectedIndex == 0)
        {
            lblRequiredCompany.Text = Resource.MessInformSelectAValue;
            lblRequiredCompany.Visible = true;
            return;
        }
        lblRequiredCompany.Visible = false; 
        if(drlGroup.SelectedIndex==0)
        {
            lblRequiredGroup.Text = Resource.MessInformSelectAValue;
            lblRequiredGroup.Visible = true; 
        }
        lblRequiredGroup.Visible = true;
        employeeId = GetEmployeeId();
        _employee = _StockService.GetEmployee(employeeId);
        if (_employee == null ) _employee = new Employee();
        _employee.DepartmentId = int.Parse(drlDepartment.SelectedValue);
        _employee.CompanyId = int.Parse(drlCompany.SelectedValue);
        _employee.EmployeeName = txtEmpName.Text;
        _employee.EmailAddress = txtEmpEmail.Text;
        _employee.GroupId = Int32.Parse(drlGroup.SelectedValue);
        if (chb1.Checked)
        {
            _employee.ReportInvisible = true;
        }
        else _employee.ReportInvisible = false;

        _employee.UserName = txtLogin.Text;
        _employee.Payroll = txtPayroll.Text;
        _employee.FullDomain = txtDomain.Text;
        _ReferenceDataService.SaveEmployee(_employee);
        _flagNew = false;
        lblPermission.Text = Resource.MessageValidate;  
        
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        lblPermission.Text = "";
        if (!new Utility().TestPermissions(Resource.PermissionAddEmployee))
        {
            lblPermission.Text = Resource.Permission;
            return;
        }
        ClearEmployees();
        btnAdd.Enabled = false;
        _flagNew = true;
        Session["refresh"] = true;
        
    }
    public  void GetSession()
    {
        Session["employeeId"] = 2;
    }
    private  void ClearEmployees()
    {
        txtDomain.Text = string.Empty;
        txtEmpEmail.Text = string.Empty;
        txtEmpName.Text = string.Empty;
        txtLogin.Text = string.Empty;
        txtPayroll.Text = string.Empty;
        chb1.Checked = false;
        drlGroup.SelectedIndex = 0;
        drlDepartment.SelectedIndex = 0;
        drlCompany.SelectedIndex = 0;
     
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        employeeId = GetEmployeeId();
        if (employeeId != 0) DisplayDetailInfo(employeeId);
        btnAdd.Enabled = true;
        _flagNew = false;
    }
}
